package com.jjh.back.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jjh.common.pojo.InfoEnum;
/**
 * 登录后访问的页面
 * 只是管理员访问的界面 
 */
import com.jjh.common.servlet.BaseServlet;
import com.jjh.common.util.ExecutorsUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.common.util.RedisUtil;
import com.jjh.users.pojo.AStatRes;
import com.jjh.users.service.IAuthDbService;
import com.jjh.users.service.IUsersDbService;
import com.jjh.users.service.IUsersOperService;
import com.jjh.users.util.UsersCallableUtil;

@Controller
@RequestMapping("/back/stat")
public class StatBackServlet extends BaseServlet
{
	@Autowired
	private IAuthDbService authDbService ;
	@Autowired
	private IUsersDbService usersDbService ;
	@Autowired
	private IUsersOperService usersOperService;
	
	/**
	 * 日志统计
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/logStatList")
	private String logStatList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String,Object> condMap = this.proccedSearchCond(request);
		condMap.put("orderBy", "currDateAsc");
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		/* 直接查询结果表 */
		List<AStatRes> list = this.usersDbService.findCondListStatResService(pageInfoUtil, condMap);
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/* 服务端跳转 */
		return "back/stat/logStatList" ;
	}
	
	/**
	 * 实时统计
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/logStatRealTimeList")
	private String logStatRealTimeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		JSONObject resultJSON = new JSONObject();
		/* 返回值是字符串 */
		Object obj = this.redisUtil.get(RedisUtil.KEY_USERS_DATA);
		try
		{
			if(obj != null) {
				resultJSON = (JSONObject) JSON.parse(obj + "");
			}
		} catch (Exception e)
		{
		}
		request.setAttribute("list", resultJSON);
		/* 服务端跳转 */
		return "back/stat/logStatRealTimeList" ;
	}
	
	/**
	 * 老的处理方法(数据量小的时候可以,大的时候不行)
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/logStatListOld")
	private String logStatListOld(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String,Object> condMap = this.proccedSearchCond(request);
		condMap.put("operType", "dataStat");
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 登录类型 */
		String logType = request.getParameter("logType");
		if(logType == null)
		{
			logType = "" ;
		}
		condMap.put("logType", logType);
		request.setAttribute("logType", logType);
		
		if(condMap.get("st") == null || condMap.get("ed") == null)
		{
			/* 只要时间不选择，默认一个月 */
			Calendar calendar = Calendar.getInstance(); 
			Date edDate = calendar.getTime();
			calendar.add(Calendar.MONTH, -1);
			Date stDate = calendar.getTime() ; 
			condMap.put("st", stDate);
			condMap.put("ed", edDate);
		}
		
		List<Map<String, Object>> list = this.usersDbService.statLogListService(pageInfoUtil ,condMap);
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/* 服务端跳转 */
		return "back/stat/logStatList" ;
	}
	
	/**
	 * 手动计算，打开添加页面
	 * 这个页面有两个时间;(开始时间和结束时间)
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/statResInsert")
	private String statResInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 服务端跳转 */
		return "back/stat/statResInsert" ;
	}
	
	/**
	 * 手动操作添加提交页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping(value = "/statResInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	private String statResInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		JSONObject resultJSON = new JSONObject();
		/* 接收时间 */
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		/* 2021-03-10 */
		Date stDate = null ; 
		/* 2021-03-20 */
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/* 转换时间 */
			stDate = this.dateUtil.strToDate(st);
			edDate = this.dateUtil.strToDate(ed);
			
			UsersCallableUtil usersCallableUtil = new UsersCallableUtil();
			/* 设置值 */
			usersCallableUtil.setUsersOperService(this.usersOperService);
			Map<String,Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("stDate", stDate);
			paramsMap.put("edDate", edDate);
			/* 设置值 */
			usersCallableUtil.setParamsMap(paramsMap);
			usersCallableUtil.setOperType("statResBatchInsert");
			/* 启动线程 */
			ExecutorsUtil.submit(usersCallableUtil);
			info = "线程已经启动,请稍后看结果" ; 
		}else
		{
			info = "请输入时间" ; 
		}
		
		resultJSON.put("info", info);
		resultJSON.put("code", InfoEnum.INFO_SUCCESS.getCode());
		/* 服务端跳转 */
		return resultJSON.toJSONString() ;
	}
}
